<?php
namespace Action;
use HY\Action;
!defined('HY_PATH') && exit('HY_PATH not defined.');
class Setip extends HYBBS {
    public function __construct(){
        parent::__construct();
    }
    public function blink(){
        $ethname= trim(X('post.ethname'));
        $speed = M("Sysinfo")->execCmd('sudo /usr/sbin/ethtool -p '.$ethname.' 5');
        $this->json(
            [
                'state'=>true,
                'info'=>'闪烁5秒'
            ]
        );
    }
    public function setIp(){
        $ip2= trim(X('post.ip2'));
        $ip = explode("/",$ip2);
        if(count($ip)==2){
            //ip 和掩码
            $ip=$ip[0];
            if($ip == "192.168.1.1"||$ip == "127.0.0.1"){
                $this->json(array('error'=>false,'info'=>'非法ip'));
            }
        }else{
            $this->json(array('error'=>false,'info'=>'ip输入地址错误'));
        }
        $manage = trim(X('post.manage',""));
        $ethname= trim(X('post.ethname'));
        //$mask = trim(X("post.mask"));
        $gateway = trim(X("post.gateway"));
        //针对显示的第一个网口 管理功能（不是192.168.1.1口） 同时也是TCP网关加速业务口 先修改配置文件 如果失败 直接返回
        if($manage == "manage"){
            //修改网站域名
            $content = @file_get_contents(INDEX_PATH . 'Conf/config.php');
            if($content === false)
                $this->json(array('error'=>false,'info'=>'/Conf读取失败'));
            $pose_DOMAIN_NAME =  strpos($content,"'DOMAIN_NAME' =>");
            $pose_dou1 = strpos($content,"'",$pose_DOMAIN_NAME+17);
            $pose_dou2 = strpos($content,"'",$pose_dou1+1);
            $dn_old=substr($content,$pose_dou1,intval($pose_dou2-$pose_dou1+1));
            $content = str_replace($dn_old,"'http://".$ip."'",$content);
            if(@file_put_contents(INDEX_PATH . 'Conf/config.php',$content) === false)
                $this->json(array('error'=>false,'info'=>'/Conf无写入失败'));
            //修改iptables
            $content = @file_get_contents(INDEX_PATH . 'FAST/iptables.sh');
            if($content === false)
                $this->json(array('error'=>false,'info'=>'/tcp加速网关配置文件读取错误'));
            $pose_DOMAIN_NAME =  strpos($content,"tcp -d");
            $pose_dou1 = strpos($content," ",$pose_DOMAIN_NAME+6);
            $pose_dou2 = strpos($content,",",$pose_dou1+1);
            $dn_old=substr($content,$pose_dou1+1,intval($pose_dou2-$pose_dou1-1));
           
            $content = str_replace($dn_old,$ip,$content);
           
            //$this->json(array('error'=>false,'info'=>$content));    
            if(@file_put_contents(INDEX_PATH . 'FAST/iptables.sh',$content) === false)
                $this->json(array('error'=>false,'info'=>'/tcp加速网关配置文件写入失败'));    
        }

        M("Sysinfo")->execCmd('nmcli connection mod '.$ethname.' ipv4.addr "'.$ip2.'"');
        M("Sysinfo")->execCmd('nmcli connection mod '.$ethname.' ipv4.gateway "'.$gateway.'"');
        M("Sysinfo")->execCmd('nmcli connection reload ');
        M("Sysinfo")->execCmd('nmcli connection down '.$ethname);
        M("Sysinfo")->execCmd('nmcli connection up '.$ethname);
        $this->json(["error"=>true,"info"=>"已修改" ,"content"=>'nmcli connection mod '.$ethname.' ipv4.addr "'.$ip2.'"']);
    }
    public function Index(){
       $ips =  M("Sysinfo")->getIps();
        $resIps = [];
        foreach($ips as $k=>$v){
            if(strstr($v["ethname"],"vir")||$v["ip"]=="192.168.1.1"||$v["ip"]=="127.0.0.1"){

            }else{
                $hInfo = M("Sysinfo")->execCmd('nmcli connection show '.$v["ethname"].' |grep -E \'(ipv4.addresses|ipv4.gateway)\'');
                $hInfo = preg_replace("/\s{2,}/",' ',$hInfo);
                $hInfo = explode("\n",trim($hInfo));
                //$v["mark"]=explode(" ",$hInfo[0])[1];
                if(count($hInfo) >=2){
                    $v["ip2"]= explode(" ",$hInfo[0])[1];
                    $v["gateway"]=explode(" ",$hInfo[1])[1];
                }else{
                    $v["ip2"]= "0.0.0.0/24";
                    $v["gateway"]="0.0.0.0";
                }

                array_push($resIps,$v);
            }

        }
        $this->v('resIps',$resIps);
        $this->view = 'FAST';
        $this->display('setip');
    }


    //{hook a_index_fun}
}
